package com.hlkj.baojin.dao.repository.utils;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author liu-zhong-xin
 * @createDate 2018/5/25 0025
 */
public class DaoQueryUtils {
    public static void query(Query query) {
        query.with(new Sort(Sort.Direction.DESC, "createTime"));
    }

    public static void queryUpd(Query query) {
        query.with(new Sort(Sort.Direction.DESC, "updateTime"));
    }

    public static <T> void updateBean(Field[] declaredFields, T updateO, Update update) {

        for (Field declaredField : declaredFields) {
            Object field = getField(declaredField, updateO);
            if(field!=null){
                String type = declaredField.getGenericType().getTypeName();
                //处理list更新(list内全部更新)
                if(declaredField.getGenericType().getTypeName().contains("<")) {
                    type = declaredField.getGenericType().getTypeName().substring(0, declaredField.getGenericType().getTypeName().indexOf("<"));
                }
                switch (type){
                    case "java.lang.String":
                        update.set(declaredField.getName(),((String) field));
                        break;
                    case "java.lang.Boolean":
                        update.set(declaredField.getName(),((Boolean) field));
                        break;
                    case "java.lang.Integer":
                        update.set(declaredField.getName(),((Integer) field));
                        break;
                    case "java.lang.Double":
                        update.set(declaredField.getName(),((Double) field));
                        break;
                    case "java.lang.Long":
                        update.set(declaredField.getName(),((Long) field));
                        break;
                    case "java.lang.Float":
                        update.set(declaredField.getName(),((Float) field));
                        break;
                    case "java.lang.Short":
                        update.set(declaredField.getName(),((Short) field));
                        break;
                    case "java.lang.Byte":
                        update.set(declaredField.getName(),((Byte) field));
                        break;
                    case "java.lang.Character":
                        update.set(declaredField.getName(),((Character) field));
                        break;
                    case "java.util.List":
                        update.set(declaredField.getName(), ((List) field));
                        break;
                    default:
                        break;
                }
            }
        }
    }

    public static void queryBean(Field[] declaredFields,Object o,Query query){
        for (Field declaredField : declaredFields) {
            Object field = getField(declaredField, o);
            if(field!=null){
                switch (declaredField.getGenericType().getTypeName()){
                    case "java.lang.String":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((String) field));
                        break;
                    case "java.lang.Boolean":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Boolean) field));

                        break;
                    case "java.lang.Integer":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Integer) field));
                        break;
                    case "java.lang.Double":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Double) field));
                        break;
                    case "java.lang.Long":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Long) field));
                        break;
                    case "java.lang.Float":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Float) field));
                        break;
                    case "java.lang.Short":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Short) field));
                        break;
                    case "java.lang.Byte":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Byte) field));
                        break;
                    case "java.lang.Character":
                        query.addCriteria(Criteria.where(declaredField.getName()).is((Character) field));
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private static Object getField(Field field,Object o){
        field.setAccessible(true);
        Object i = null;
        try {
            i = field.get(o);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return i;
    }
}
